// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   DownloadFileServlet.java

package com.hfstu.file.servlet;

import com.hfstu.file.FileServerInfo;
import com.hfstu.file.dataaccess.service.IUploadFileService;
import com.hfstu.file.model.UploadFile;
import com.hfstu.file.server.FileServerContext;
import com.hfstu.file.server.ResponseStatus;
import com.hfstu.file.utils.AccessPolicyUtil;
import com.hfstu.file.utils.ContentTypeUtil;
import java.io.*;
import java.util.Properties;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class DownloadFileServlet extends HttpServlet
{

	private static final String ATTACHMENT_CONTENT = "attachment";
	private static final String INLINE_CONTENT = "inline";
	private static final String DEFAULT_CONTENTTYPE = "application/octet-stream";
	private static final String PUBLIC_HEADER = "public";
	private static final String PRAGMA_HEADER_KEY = "Pragma";
	private static final String BINARY_ENCODE = "binary";
	private static final String CACHE_CONTROL__HEADER_KEY = "Cache-Control";
	private static final String MUST_REVALIDATE_POST_CHECK_0_PRE_CHECK_HEADER = "must-revalidate, post-check=0, pre-check=0";
	private static final String CONTENT_TRANSFER_ENCODING_HEADER_KEY = "Content-Transfer-Encoding";
	private static final String CONTENT_DISPOSITION_HEADER = "Content-Disposition";
	private static final String ISO8859_1_ENCODE = "ISO8859_1";
	private static final String GBK_ENCODE = "gbk";
	private static final long serialVersionUID = 0x30e9fdf5f706252dL;
	private Properties contentTypeProps;
	private Logger _log;
	private static final int DEFAULT_BUFFER_SIZE = 0x80000;

	public DownloadFileServlet()
	{
		contentTypeProps = ContentTypeUtil.getInstance().getProperties();
		_log = LoggerFactory.getLogger(com/hfstu/file/servlet/DownloadFileServlet);
	}

	public void service(HttpServletRequest req, HttpServletResponse res)
	{
		String openmode;
		UploadFile uploadFile;
		InputStream is;
		String fileName;
		String uuid = req.getParameter("uuid");
		if (StringUtils.isBlank(uuid))
		{
			_log.warn("uuid is empty");
			return;
		}
		boolean canDownload = AccessPolicyUtil.validateDownloadPolicy(req);
		if (!canDownload)
		{
			_log.warn("can not download in any policy");
			writeResponse(res, ResponseStatus.Denied.toString());
			return;
		}
		openmode = req.getParameter("openmode");
		uploadFile = FileServerContext.getInstance().getUploadFileService().getById(uuid);
		if (uploadFile == null)
		{
			_log.warn((new StringBuilder("file not found in database,uuid=")).append(uuid).toString());
			writeResponse(res, ResponseStatus.Error.toString());
			return;
		}
		is = null;
		fileName = uploadFile.getFileName();
		try
		{
			is = new BufferedInputStream(new FileInputStream(FileServerInfo.getInstance().getAbsolutePath(uploadFile)));
			setFileInfo(res, fileName, openmode);
			copyLarge(is, res.getOutputStream());
			break MISSING_BLOCK_LABEL_277;
		}
		catch (IOException e)
		{
			writeResponse(res, ResponseStatus.Error.toString());
			_log.error(e.getMessage());
		}
		if (is != null)
			try
			{
				is.close();
			}
			catch (IOException e)
			{
				_log.error("close fis failure.", e);
			}
		break MISSING_BLOCK_LABEL_305;
		Exception exception;
		exception;
		if (is != null)
			try
			{
				is.close();
			}
			catch (IOException e)
			{
				_log.error("close fis failure.", e);
			}
		throw exception;
		if (is != null)
			try
			{
				is.close();
			}
			catch (IOException e)
			{
				_log.error("close fis failure.", e);
			}
	}

	private void writeResponse(HttpServletResponse res, String info)
	{
		try
		{
			res.getOutputStream().write(info.getBytes());
		}
		catch (IOException e)
		{
			_log.error("write bytes failure.", e);
		}
	}

	private String getSuffix(String fileName)
	{
		int index = fileName.lastIndexOf('.');
		if (index >= 0)
			return fileName.substring(index + 1);
		else
			return "";
	}

	private String getContentType(String suffix)
	{
		String content = contentTypeProps.getProperty(suffix);
		if (content != null)
			return content;
		else
			return "application/octet-stream";
	}

	private String getContentDisposition(String openmode, String suffix)
	{
		if (openmode != null && openmode.equals("inline") || ContentTypeUtil.getInstance().isInlineType(suffix))
			return "inline";
		else
			return "attachment";
	}

	private void setFileInfo(HttpServletResponse res, String fileName, String openmode)
		throws IOException
	{
		try
		{
			fileName = new String(fileName.getBytes("gbk"), "ISO8859_1");
		}
		catch (UnsupportedEncodingException e1)
		{
			_log.error("convert code error", e1);
		}
		String suffix = getSuffix(fileName);
		String content = getContentType(suffix);
		res.setContentType(content);
		String contentDisposition = getContentDisposition(openmode, suffix);
		res.addHeader("Content-Disposition", (new StringBuilder(String.valueOf(contentDisposition))).append("; filename=\"").append(fileName).append("\"").toString());
		res.setHeader("Content-Transfer-Encoding", "binary");
		res.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
		res.setHeader("Pragma", "public");
	}

	private void copyLarge(InputStream input, OutputStream output)
		throws IOException
	{
		byte buffer[] = new byte[0x80000];
		for (int n = 0; -1 != (n = input.read(buffer));)
			output.write(buffer, 0, n);

	}
}
